package com.rs.game.player.content.commands;

import com.rs.Settings;
import com.rs.game.Animation;
import com.rs.game.World;
import com.rs.game.WorldTile;
import com.rs.game.player.Player;
import com.rs.game.player.content.Commands;
import com.rs.game.player.content.Magic;
import com.rs.game.player.content.TicketSystem;
import com.rs.game.tasks.WorldTask;
import com.rs.game.tasks.WorldTasksManager;
import com.rs.utils.SerializableFilesManager;
import com.rs.utils.ShopsHandler;
import com.rs.utils.Utils;

public class Support {
	
@SuppressWarnings("null")
public static boolean processCommand(final Player player, String[] cmd, boolean console, boolean clientCommand) {
	String name;
	Player target;
	String message;
	
	if (clientCommand) {
		
	} else {
		switch (cmd[0]) {
		case "coords":
			player.getPackets().sendGameMessage("X: " + player.getX() + " Y: " + player.getY() + " Plane: " + player.getPlane());
			player.getPackets().sendGameMessage("Region ID: " + player.getRegionId() + " Region X: " + player.getChunkX() + " Region Y: " + player.getChunkY());
			return true;
		/*
		 * Kick a player out of the server
		 * 
		 * Usage: ::kick playerName
		 * Example: ::kick swirl
		 */
		case "kick":
			name = "";
			for (int i = 1; i < cmd.length; i++) {
				name += cmd[i] + ((i == cmd.length - 1) ? "": " ");
			}
			target = World.getPlayerByDisplayName(name);
			if (target != null) {
				target.getSession().getChannel().close();
				player.getPackets().sendGameMessage("You have kicked " + Utils.formatPlayerNameForDisplay(name) + ".");
			} else {
				player.getPackets().sendGameMessage(Utils.formatPlayerNameForDisplay(name) + " is either offline, or doesn't exist.");
			}
			return true;
			
		/*
		 * Reloads the shops without restarting
		 * 
		 * Usage: ::reloadshops Example: reloadshops
		 */
		case "reloadshops":
			ShopsHandler.init();
			World.sendWorldMessage("<col=05f4d0> The Shops have been reloaded!", false);
			return true;
		
		case "title":
			try {
					player.getAppearence().setTitle(345);	
			} catch (NumberFormatException e) {
				player.getPackets().sendGameMessage("Use: ::title id");
			}
			return true;	
		
		case "tele":
			if (cmd.length < 3) {
				player.getPackets().sendPanelBoxMessage("Use: ::tele coordX coordY");
				return true;
			}
			try {
				player.resetWalkSteps();
				player.setNextWorldTile(new WorldTile(Integer.valueOf(cmd[1]), Integer.valueOf(cmd[2]), cmd.length >= 4 ? Integer.valueOf(cmd[3]) : player.getPlane()));
			} catch (NumberFormatException e) {
				player.getPackets().sendPanelBoxMessage("Use: ::tele coordX coordY plane");
			}
			return true;	
		/*
		 * Jail a player for 24 hours
		 * 
		 * Usage: ::jail playerName
		 * Example: ::jail swirl
		 */
		case "jail":
			name = "";
			for (int i = 1; i < cmd.length; i++)
				name += cmd[i] + ((i == cmd.length - 1) ? "" : " ");
			target = World.getPlayerByDisplayName(name.replaceAll(" ", "_"));
			if (target != null) {
				target.setJailed(Utils.currentTimeMillis()
						+ (24 * 60 * 60 * 1000));
				target.getControlerManager().startControler("JailControler");
				target.getPackets().sendGameMessage(
						"You've been jailed for 24 hours by "+player.getDisplayName()+".");
				player.getPackets().sendGameMessage(
						"You have jailed " + target.getDisplayName()
								+ " for 24 hours.");
			} else {
				player.getPackets().sendGameMessage(
						"Couldn't find player " + name + ".");
			}
			return true;
		
		
		/*
		 * Mute a player for 48 hours
		 * 
		 * Usage: ::mute playerName
		 * Example: ::mute swirl
		 */
		case "mute":
			name = "";
			for (int i = 1; i < cmd.length; i++) {
				name += cmd[i] + ((i == cmd.length - 1) ? "" : " ");
			}
			target = World.getPlayerByDisplayName(name);
			if (target != null) {
				if (target.getRights() >= 2) {
					player.getPackets().sendGameMessage("You can't mute administrators!");
					return true;
				}
				target.lock(15);
				final Player _target = target;
				WorldTasksManager.schedule(new WorldTask() {
					@Override
					public void run() {
						_target.setNextAnimation(new Animation(-1));
						_target.setNextWorldTile(player);

					}
				}, 5);
				target.setBlackmarks(target.getBlackmarks() + 0.5);
				target.setMuted(Utils.currentTimeMillis() + (48 * 60 * 60 * 1000));
				target.getPackets().sendGameMessage("You've been temporarily muted for 48 hours by " + Utils.formatPlayerNameForDisplay(player.getUsername()));
				player.getPackets().sendGameMessage("You muted " + Utils.formatPlayerNameForDisplay(name) + " for 48 hours.");
			} else {
				name = Utils.formatPlayerNameForProtocol(name);
				if (!SerializableFilesManager.containsPlayer(name)) {
					player.getPackets().sendGameMessage("Account name " + Utils.formatPlayerNameForDisplay(name) + " doesn't exist.");
					return true;
				}
				target.lock(15);
				target = SerializableFilesManager.loadPlayer(name);
				target.setUsername(name);
				target.setBlackmarks(target.getBlackmarks() + 0.5);
				target.setMuted(Utils.currentTimeMillis() + (48 * 60 * 60 * 1000));
				player.getPackets().sendGameMessage("You muted " + Utils.formatPlayerNameForDisplay(name) + " for 48 hours.");
				SerializableFilesManager.savePlayer(target);
			}
			return true;
			
		/*
		 * Send a staff yell across the server for staff only
		 * 
		 * Usage: ::staffyell message goes here
		 * Example: ::staffyell this is a staff yell
		 */
		case "staffyell":
			message = "";
			for (int i = 1; i < cmd.length; i++) {
				message += cmd[i] + ((i == cmd.length - 1) ? "" : " ");
			}
			Commands.sendYell(player, Utils.fixChatMessage(message), true);
			return true;
			
		/*
		 * Send a player home
		 * 
		 * Usage: ::sendhome playerName
		 * Example: ::sendhome swirl
		 */
		case "sendhome":
			name = "";
			for (int i = 1; i < cmd.length; i++) {
				name += cmd[i] + ((i == cmd.length - 1) ? "" : " ");
			}
			target = World.getPlayerByDisplayName(name);
			if (target != null) {
				target.unlock();
				target.getControlerManager().forceStop();
				if (target.getNextWorldTile() == null) {
					target.setNextWorldTile(Settings.RESPAWN_PLAYER_LOCATION);
				}
				player.getPackets().sendGameMessage("You have sent " + Utils.formatPlayerNameForDisplay(name) + " home.");
				target.getPackets().sendGameMessage(Utils.formatPlayerNameForDisplay(player.getUsername()) + " has sent you home.");
			} else {
				player.getPackets().sendGameMessage(Utils.formatPlayerNameForDisplay(name) + " is either offline, or doesn't exist.");
			}
			return true;
			
		/*
		 * Answer an open ticket
		 * 
		 * Usage: ::ticket
		 * Example: ::ticket
		 */
		case "ticket":
			TicketSystem.answerTicket(player);
			return true;
			
		/*
		 * Close the answered ticket
		 * 
		 * Usage: ::finishticket
		 * Example: ::finishticket
		 */
		case "finishticket":
			TicketSystem.removeTicket(player);
			return true;
			
		/*
		 * Teleport to the staff area
		 * 
		 * Usage: ::staffzone
		 * Example: ::staffzone
		 */
		case "sz": 
		case "staffzone":
			Magic.sendNormalTeleportSpell(player, 0, 0, new WorldTile(2847, 5150, 0));
			return true;
		}
	}
	return false;
}
}
